package com.bishe.cyh.service.Impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bishe.cyh.compont.Decompression;
import com.bishe.cyh.config.DirectRabbitConfig;
import com.bishe.cyh.mapper.TaskDataChoiceMapper;
import com.bishe.cyh.mapper.TaskMapper;
import com.bishe.cyh.mapper.TaskstopMapper;
import com.bishe.cyh.mode.*;
import com.bishe.cyh.service.TaskService;
import com.bishe.cyh.utils.ListSort;
import com.bishe.cyh.utils.SnowIdUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author: Poppin
 * @Date: 2022/2/28 16:06
 * @Version: 1.0
 */
@Service
public class TaskServiceImpl implements TaskService {

    private TaskDataChoiceMapper taskDataChoiceMapper;
    private TaskMapper taskMapper;
    private Decompression decompression;
    private TaskstopMapper taskstopMapper;
    private RabbitTemplate rabbitTemplate;

    @Autowired
    public TaskServiceImpl(TaskDataChoiceMapper taskDataChoiceMapper,TaskMapper taskMapper,Decompression decompression,TaskstopMapper taskstopMapper,RabbitTemplate rabbitTemplate){
        this.rabbitTemplate=rabbitTemplate;
        this.taskDataChoiceMapper = taskDataChoiceMapper;
        this.taskMapper = taskMapper;
        this.decompression=decompression;
        this.taskstopMapper=taskstopMapper;
    }


    @Override
    public Result Datachoice() {
        List<Datachoice> list = taskDataChoiceMapper.selectList(null);
        return new Result(0,"成功",list);
    }

    @Override
    public Result TaskSubmit(TaskSubmit taskSubmit) {

        taskSubmit.setStatus(0);
        long d = System.currentTimeMillis();
        Date date = new Date();
        date.setTime(d);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        taskSubmit.setStarttime(simpleDateFormat.format(date));
        taskSubmit.setJgid(SnowIdUtils.uniqueLongHex());
        taskMapper.insert(taskSubmit);
        SparkMode sparkMode = new SparkMode();
        sparkMode.setDatachoice(taskSubmit.getDatachoice());
        sparkMode.setA(taskSubmit.getRecogin()/100.0f);
        sparkMode.setSnowid(taskSubmit.getJgid());
        String json = JSON.toJSONString(sparkMode);
        rabbitTemplate.convertAndSend(DirectRabbitConfig.Queueu,json);
//        decompression.Spark(taskSubmit.getDatachoice(),taskSubmit.getRecogin()/100.0f,taskSubmit.getJgid());
        return new Result<Messqge>(0,"成功",new Messqge("任务提交提交成功等待处理"));
    }

    @Override
    public Result GetTaskList() {
        List<TaskSubmit> list = taskMapper.selectList(null);
        ListSort.TaskList(list);
        return new Result(0,"成功",list);
    }

    @Override
    public Result DeleteTask(String id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("jgid",id);
        int count = taskMapper.delete(queryWrapper);
        QueryWrapper queryWrapper1 = new QueryWrapper();
        queryWrapper1.eq("id",id);
        int count1 = taskstopMapper.delete(queryWrapper1);
        if (count!=0&&count1!=0){
            return new Result<Messqge>(0,"成功",new Messqge("删除成功"));
        }
        return new Result<Messqge>(0,"失败",new Messqge("删除失败"));
    }
}
